import { getPageListData } from '@/service/main/system/system'
import { IRootState, IStoreType } from './types'
import { createStore, Store, useStore as useVuexStore } from 'vuex'
import loginModule from './login/login'
import systemModule from './main/system/system'
import dashboardModule from './main/analysis/dashboard'

const store = createStore<IRootState>({
  state() {
    return {
      name: 'testvue3',
      age: 123,
      entireDepartment: [],
      entireRole: [],
      entireMenu: []
    }
  },
  mutations: {
    changeEntireDepartment(state, list) {
      state.entireDepartment = list
    },
    changeEntireRole(state, list) {
      state.entireRole = list
    },
    changeEntireMenu(state, list) {
      state.entireMenu = list
    }
  },
  getters: {},
  actions: {
    async getInitialDataAction({ commit }) {
      //请求部门和角色数据
      const DepartmentResult = await getPageListData('/department/list', {
        offset: 0,
        size: 1000
      })
      const { list: DepartmentList } = DepartmentResult.data
      commit('changeEntireDepartment', DepartmentList)

      const roleResult = await getPageListData('/role/list', {
        offset: 0,
        size: 1000
      })
      const { list: roleList } = roleResult.data
      commit('changeEntireRole', roleList)

      const menuResult = await getPageListData('/menu/list', {})
      const { list: menuList } = menuResult.data
      commit('changeEntireMenu', menuList)
    }
  },
  modules: {
    login: loginModule,
    system: systemModule,
    dashboard: dashboardModule
  }
})
// declare module '@vue/runtime-core' {
//   interface ComponentCustomProperties {
//     // eslint-disable-next-line @typescript-eslint/no-explicit-any
//     $store: Store<any>
//   }
// }

export function setupStore() {
  store.dispatch('login/loadLocalLogin')
}

export function useStore(): Store<IStoreType> {
  return useVuexStore()
}
export default store
